<template>
  <!-- 最大盒子 -->
  <s-layout :tabbarshow="false" id="layout" navbar="normal" :title="''" :autoBack="true" leftIconColor="$uni-text-color"
    :bgColor="'rgba(0,0,0,0)'">

    <!-- 使用web-view组件加载PDF.js -->
    <web-view :src="url"   @message="getMessage($event,name)" ></web-view>
   
  </s-layout>

</template>
<script setup>
import sheep from '@/sheep';
import { ref, reactive } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { baseUrl } from "@/sheep/config";
import {
	isEmpty
} from 'lodash';
const C_department_id = ref()
const url = ref();
const introduction = reactive({
    contract: '',
    id:'',
    share_id:''
})
const paroptions = ref({
    salary:'',
    probation_salary:'',
    city:'',
    position:'',
    probation_start:'',
    probation_end:'',
    department_id:''
})

const paramss = ref()
const group_id = ref()
const name = ref()
onLoad((options) => {
  introduction.id = options.id
  paramss.value = JSON.parse(options.params);
  paroptions.value = JSON.parse(options.entry);
  C_department_id.value = options.department_id
  group_id.value = options.group_id
  name.value = options.name
  introduction.contract = options.url
  if (options.url !== '' ) {
      url.value = baseUrl + '/uploads/pdf-h5-master/pdfH5.html?contract=' + encodeURIComponent(options.url)
  } else {
    request_contact()
  }
})
const seal = ref()
const enrollment_sign_post = ref()
const getMessage = (e,a) => {
   seal.value = e.detail.data[0].image.replace(/data.+?;base64,/, "")
    if(a == 'enrollment'){
      setTimeout(async() => {
      enrollment_sign_post.value = {
          contract:introduction.contract,
          department_id:paroptions.value.department_id,
          seal:seal.value,
          is_contact:paroptions.value.is_contact,
          id:introduction.id,
          share_id:introduction.share_id
      }
      if(paramss.value.is_contact == 0) {
        enrollment_sign_post.value = { ...enrollment_sign_post.value , group_id: group_id.value }
      }
      // 入职申请（2-个人签字）
      const res = await sheep.$api.user.enrollmentSignature(enrollment_sign_post.value)
      uni.showLoading({ title: '加载中' });   
      if(res.code == 1){
        if(paramss.value.is_contact == 1){
              request_contact()
            } else {
              uni.hideLoading()
              uni.showModal({
                    title: '提示',
                    content: res.msg,
                    showCancel: false,
                      success: function (results) {
                          if (results.confirm) {
                            sheep.$store('user').getInfo();
                            sheep.$router.go('/pages/user/user/user')
                          } 
                      }
                  });
            }
      }else{
        uni.showModal({
                title: '提示',
                content: res.msg,
                showCancel: false,
                  success: function (results) {
                      if (results.confirm) {
                        sheep.$store('user').getInfo();
                        sheep.$router.go('/pages/user/user/user')
                      } 
                  }
              });
      }
      },500)
    
      
    }else if(a == 'contact'){
     
      setTimeout(async() => {
      let paramData ={
            contract: introduction.contract,
            seal:seal.value,
            department_id:C_department_id.value,
            group_id:group_id.value,
            id:introduction.id,
            share_id:introduction.share_id
          }
          const ress = await sheep.$api.user.contactSignature(paramData)
             if(ress.code == 1){
                uni.showModal({
                title: '提示',
                content: ress.msg,
                showCancel: false,
                  success: function (results) {
                      if (results.confirm) {
                        sheep.$store('user').getInfo();
                        sheep.$router.go('/pages/user/user/user')
                      } 
                  }
              });
              uni.hideLoading()
          }else{
            uni.hideLoading()
            sheep.$helper.toast(ress.msg);
          }
        },500)
    }

  
}


const request_contact = async () =>   {
  if(paroptions.value.is_contact == 1){
          let data ={
          hukou_type:paramss.value.hukou_type ,
          householdaddress:paramss.value.householdaddress,
          address:paramss.value.address,
          city:paroptions.value.city,
          position:paroptions.value.position,
          salary:paroptions.value.salary,
          probation_salary:paroptions.value.probation_salary,
          probation_start:paroptions.value.probation_start,
          probation_end:paroptions.value.probation_end,
          year:paroptions.value.year,
        }
        //入职申请（3-请求劳务合同）
        const result = await sheep.$api.user.requestContact(data)
        if(result.code == 1){
          introduction.contract = result.data.url
          sheep.$router.go('/subpackage/user/user/Invite_colleague_pdf',{
            url:result.data.url,
            params:JSON.stringify(paramss),
            entry:JSON.stringify(paroptions.value),
            department_id:paroptions.value.department_id,
            group_id:group_id.value,
            name: 'contact',
            id:introduction.id,
            share_id:introduction.share_id
            })
        } else {
          uni.hideLoading()
          uni.showModal({
                title: '提示',
                content: result.msg,
                showCancel: false,
                  success: function (results) {
                      if (results.confirm) {
                        sheep.$store('user').getInfo();
                        sheep.$router.go('/pages/user/user/user')
                      } 
                  }
              });
        }
        // sheep.$helper.toast(result.msg);
        } else {
          uni.hideLoading()
          uni.showModal({
                title: '提示',
                content: result.msg,
                showCancel: false,
                  success: function (results) {
                      if (results.confirm) {
                        sheep.$store('user').getInfo();
                        sheep.$router.go('/pages/user/user/user')
                      } 
                  }
              });
        }
}

</script>